
using System;
using System.IO;

namespace CustomStreams
{

	public class UpperCaseStream : Stream
	{
			
		Stream _stream;

		public UpperCaseStream(Stream stream)
		{
			_stream = stream;
		}

		public override bool CanRead
		{
			get { return _stream.CanRead; }
		}

		public override bool CanSeek
		{
			get { return _stream.CanSeek; }
		}

		public override bool CanWrite
		{
			get { return _stream.CanWrite; }
		}
		
		public override long Length
		{
			get { return _stream.Length; }
		}

		public override long Position
		{
			get { return _stream.Position; }
			set { _stream.Position = value; }
		}

		public override void Close()
		{
			_stream.Close();
		}

		public override void Flush()
		{
			_stream.Flush();
		}

		public override long Seek( long offset, System.IO.SeekOrigin origin)
		{
			return _stream.Seek( offset, origin );
		}

		public override void SetLength( long length )
		{
			_stream.SetLength( length );
		}

		public override int Read( byte[] buffer, int offset, int count )
		{
			int bytesRead;
			int index;

			Console.WriteLine("Read called {0} {1}", offset, count );

			// let base class do the read
			bytesRead = _stream.Read( buffer, offset, count );

			// if something was read
			if ( bytesRead > 0)
			{
				for( index = offset; index < (offset+bytesRead); index++)
				{
						if ( buffer[index] >= 'a' && buffer[index] <= 'z')
						{
							buffer[index] = (byte) (buffer[index] - 32 );
						}
				}
			}

			return bytesRead;
		}

		public override void Write( byte[] buffer, int offset, int count )
		{
			int index;

			Console.WriteLine("Write called {0} {1}", offset, count );

				_stream.Write( buffer, offset, count );

			return;

			// if something was read
			if ( count > 0)
			{
				for( index = offset; index < (offset+count); index++)
				{
						if ( buffer[index] >= 'a' && buffer[index] <= 'z')
						{
							buffer[index] = (byte) (buffer[index] - 32 );
						}
				}
			}

			// write the content 

			_stream.Write( buffer, offset, count );
		}

	}

}